package com.cloud.utils;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import com.cloud.common.constants.ExcelPoiConstants;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Map导入/导出工具类
 *
 * @author yzj
 */
public class ExcelMapUtils {

    /**
     * Map数据导入
     *
     * @param titleRows 表格标题行数
     * @param headRows  表头行数
     * @param file
     * @return
     */
    public List<Map<String, Object>> importData(int titleRows, int headRows, MultipartFile file) {
        List<Map<String, Object>> dataList = null;
        try {
            ImportParams params = new ImportParams();
            params.setTitleRows(titleRows);
            params.setHeadRows(headRows);
            dataList = ExcelImportUtil.importExcel(file.getInputStream(), Map.class, params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataList;
    }

    /**
     * Map数据导出
     *
     * @param fileName        导出文件名称
     * @param title           Excel表名称
     * @param sheetName       Sheet名称
     * @param excelEntityList 数据表头,key=字段名,name=列名称
     * @param dataList        查询数据集合
     */
    public static void export(String fileName, String title, String sheetName, List<ExcelExportEntity> excelEntityList,
                              List<Map<String, Object>> dataList) {
        try {
            // 设置Excel表单头
            List<ExcelExportEntity> entity = new ArrayList<>();
            if (ObjectUtils.isNotEmpty(excelEntityList)) {
                for (ExcelExportEntity excelEntity : excelEntityList) {
                    entity.add(excelEntity);
                }
            }
            // 创建导出参数
            ExportParams params = new ExportParams(title, sheetName, ExcelType.XSSF);
            Workbook workbook = ExcelExportUtil.exportExcel(params, entity, dataList);
            // 输出到浏览器
            HttpServletResponse response = ServletUtils.getResponse();
            OutputStream outputStream = response.getOutputStream();
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setHeader("Content-disposition", "attachment; filename="
                    + URLEncoder.encode(fileName, "UTF-8") + ExcelPoiConstants.Excel_Suffix);
            response.setContentType("application/msexcel");
            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 模板下载
     *
     * @param fileName        导出文件名称
     * @param title           Excel表名称
     * @param sheetName       Sheet名称
     * @param excelEntityList 数据表头,key=字段名,name=列名称
     */
    public static void template(String fileName, String title, String sheetName,
                                List<ExcelExportEntity> excelEntityList) {
        List<Map<String, Object>> dataList = new ArrayList<>();
        export(fileName, title, sheetName, excelEntityList, dataList);
    }

    /**
     * 设置Excel表单头
     *
     * @param mapEntity key字段名称/ value字段说明
     * @return
     */
    public static List<ExcelExportEntity> excelExportList(Map<String, String> mapEntity) {
        List<ExcelExportEntity> excelEntity = new ArrayList<>();
        for (Map.Entry<String, String> entry : mapEntity.entrySet()) {
            ExcelExportEntity exportEntity = new ExcelExportEntity(entry.getValue(), entry.getKey());
            exportEntity.setNeedMerge(true);
            exportEntity.setWidth(15);
            exportEntity.setHeight(8);
            excelEntity.add(exportEntity);
        }
        return excelEntity;
    }

}
